package com.xiangxiangjd.analyse.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiangxiangjd.analyse.bean.RepairStructure;
import com.xiangxiangjd.analyse.controller.helper.$;
import com.xiangxiangjd.analyse.bean.RepairMethod;
import com.xiangxiangjd.analyse.mapper.RepairMethodMapper;
import com.xiangxiangjd.analyse.service.RepairMethodService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiangxiangjd.analyse.bean.result.ResultPage;
import com.xiangxiangjd.analyse.controller.model.ApiResultModel;

import com.xiangxiangjd.analyse.service.RepairStructureService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 肖守凯
 * @since 2021-03-08
 */
@Service
@Slf4j
public class RepairMethodServiceImpl extends ServiceImpl<RepairMethodMapper, RepairMethod> implements RepairMethodService {


    @Autowired
    private RepairStructureService structureService;

    /**
     * 添加RepairMethod
     *
     * @param repairMethod 前端传进的参数
     * @return 返回统一信息
     */
    @Override
    public ApiResultModel addRepairMethod(RepairMethod repairMethod) {



        log.info("repairMethod:" + repairMethod.toString());

        //查询是否存在
        List<RepairMethod> list = baseMapper.selectList(new LambdaQueryWrapper<RepairMethod>().eq(RepairMethod::getStructureId, repairMethod.getStructureId()));
        if (list.size() > 0) {
            throw new RuntimeException("已经添加过 该修理方法！");
        }



        if (this.save(repairMethod)) {
            return $.success();
        } else {
            return $.error("添加失败");
        }
    }

    /**
     * 编辑跳转页面的回显数据RepairMethod
     *
     * @param id 前端传进的参数
     * @return 返回实体类
     */
    @Override
    public RepairMethod selectRepairMethodByIdRepairMethod(String id) {
        return this.getById(id);
    }


    /**
     * 编辑RepairMethod
     *
     * @param repairMethod 前端传进的参数
     * @return 返回统一信息
     */
    @Override
    public ApiResultModel editRepairMethod(RepairMethod repairMethod) {
        if (this.updateById(repairMethod)) {
            return $.success();
        } else {
            return $.error("编辑失败");
        }
    }

    /**
     * 删除一条RepairMethod
     *
     * @param ids 前端传进的ID
     * @return 返回统一信息
     */
    @Override
    public ApiResultModel delRepairMethods(List ids) {

        int i = baseMapper.deleteBatchIds(ids);

        return $.success(i);

    }

    /**
     * 分页查询RepairMethod
     *
     * @param page         页码
     * @param limit        查几条
     * @param repairMethod 前端传进的参数
     * @return 返回统一分页信息
     */
    @Override
    public ResultPage findRepairMethodByPage(Integer page, Integer limit, RepairMethod repairMethod) {
        Map
                <String, Object> paramMap = new HashMap<>();
        paramMap.put("page", (page - 1) * limit);
        paramMap.put("limit", limit);
        paramMap.put("repairMethod", repairMethod);
        List<RepairMethod> list = baseMapper.selectMyPage(paramMap);
        Integer count = baseMapper.countMyPage(paramMap);
        ResultPage<RepairMethod> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }

    @Override
    public String getSuperiorStructure(String structureId) {


        return baseMapper.getSuperiorStructure(structureId);
    }

    /**
     * 批量插入
     *
     * @param list
     */
    @Override
    @Transactional
    public void addAll(List<RepairMethod> list) {

        for (int i = 0; i < list.size(); i++) {

            /**
             * 导入结构
             *
             */

            //1.查询项目大类 是否存在
            RepairStructure item = structureService.getOne(
                    new LambdaQueryWrapper<RepairStructure>().eq(RepairStructure::getStructureName,
                            list.get(i).getItem())
                            .eq(RepairStructure::getParentId, "0")

            );
            if (item == null) {

                //添加 name 和pid
                item = structureService.addRepairStructure(new RepairStructure(list.get(i).getItem(), "0"));
                log.info("插入了item" + item.toString());
            }

            //2. 查询修理子项 匹配父级id
            RepairStructure chile = structureService.getOne(
                    new LambdaQueryWrapper<RepairStructure>()
                            .eq(RepairStructure::getStructureName, list.get(i).getChild()).eq(RepairStructure::getParentId, item.getStructureId()));
            //如果没有插入
            if (chile == null) {
                chile = structureService.addRepairStructure(new RepairStructure(list.get(i).getChild(), item.getStructureId()));
                log.info("插入了chile:" + chile.toString());
            }

            //3. 查询修理原因 和目的
            RepairStructure forRepair = structureService.getOne(
                    new LambdaQueryWrapper<RepairStructure>()
                            .eq(RepairStructure::getStructureName, list.get(i).getForRepair())
                            .eq(RepairStructure::getParentId, chile.getStructureId())
            );

            if (forRepair == null) {
                forRepair = structureService.addRepairStructure(new RepairStructure(list.get(i).getForRepair(), chile.getStructureId()));
                log.info("插入了forRepair:" + forRepair.toString());
            }

            //4.查询科目
            RepairStructure subjects = structureService.getOne(
                    new LambdaQueryWrapper<RepairStructure>()
                            .eq(RepairStructure::getStructureName, list.get(i).getSubjects())
                            .eq(RepairStructure::getParentId, forRepair.getParentId())
            );
            if (subjects == null) {
                subjects = structureService.addRepairStructure(new RepairStructure(list.get(i).getSubjects(), forRepair.getStructureId()));
                log.info("插入了subjects:" + subjects.toString());
            }


            //5查询 修理方法

            RepairStructure method = structureService.getOne(
                    new LambdaQueryWrapper<RepairStructure>()
                            .eq(RepairStructure::getStructureName, list.get(i).getMethod()).
                            eq(RepairStructure::getParentId, subjects.getStructureId())
            );
            if (method == null) {
                method = structureService.addRepairStructure(new RepairStructure(list.get(i).getMethod(), subjects.getStructureId()));
                log.info("插入了method:" + method.toString());
            }

            list.get(i).setStructureId(method.getStructureId());


            /**
             * 导入具体 修理方法
             */
            this.addRepairMethod(list.get(i));


        }
    }
}
